const host = window.location.hostname;
//账号登录按钮
let accountLoginBtn = document.querySelector('#accountLoginBtn');
//切换邮箱登录按钮
let toEmailLogin = document.querySelector("#emailLogin");
//切换账号登录按钮
let toAccountLogin = document.querySelector("#accountLogin");
// 获取发送验证码按钮节点
let sendCodeBtn = document.querySelector("#sendCode");
// 获取邮箱地址输入框节点
let emailInput = document.querySelector("#email");
// 获取验证码输入框节点
let verificationCode = document.querySelector("#code");
// 获取登录按钮节点
let loginBtn = document.querySelector("#emailLoginBtn");
//获取账号
let accountInput = document.querySelector("#account");
//获取密码
let passwordInput = document.querySelector("#password");
//切换为账号登录
toAccountLogin.addEventListener("click", (e) => {
    window.location = "../../../AVR/accountLogin";
})
//切换为邮箱登录
toEmailLogin.addEventListener("click", (e) => {
    window.location = "../../../AVR/";
})
// 点击发送验证码逻辑代码
if(sendCodeBtn != null){
    sendCodeBtn.addEventListener('click',function(){
    console.log(emailInput.value);
    // 判断用户输入的邮箱地址是否符合规范
    let flag = CheckInputEmail(emailInput.value);
    // 以及判断输入是否为空
    if(emailInput.value && flag){
        // 调用方法发送请求，向目标邮箱发送验证码
        getCode(emailInput.value);
        alert("验证码发送成功!")
        // 禁用发送按钮
        sendCodeBtn.disabled = true;
        // 设置倒计时，时间到后才可以再次发起验证码请求
        let time = 60;
        sendCodeBtn.innerHTML =  time + "s后再次发送!"
        time--;
        let myInterVal =  setInterval(()=>{
            if(time != 0){
                sendCodeBtn.innerHTML = time + "s后再次发送!"
                time--;
            }else{
                sendCodeBtn.disabled = false;
                sendCodeBtn.innerHTML = "发送"
                clearInterval(myInterVal)
            }
        },1000)
    }else{
        alert("邮箱格式错误或为空!");
    }
})}


// 点击登录按钮逻辑代码
if(loginBtn != null){
    loginBtn.addEventListener("click", function (){
    // 输入邮箱地址和验证码作为参数进行验证
    //alert("loginBtnClick");
    checkCode(emailInput.value,verificationCode.value);
})}


//注册按钮
if(accountLoginBtn != null){
    accountLoginBtn.addEventListener("click", function (){
        let password = passwordInput.value;
        let username = accountInput.value;
        axios.post(`http://${host}:8080/AVR/logins/1`,{
            userName: username,
            password:password,
            email:sessionStorage.getItem("email")
        }).then(response => {
                console.log("返回结果====>" + response.data)
            if(response.data){
                sessionStorage.setItem("email",sessionStorage.getItem("email"));
                alert("../../../AVR/User/home?email="+sessionStorage.getItem("email"));
                window.location = `http://${host}:8080/AVR/User/home?email=`+sessionStorage.getItem("email");
            }else{
                alert("验证不通过！！！")
            }
            }
        )
    })
}

// 验证邮箱输入格式是否正确
function CheckInputEmail(email){
    if (!email) {
        return false;
    }
    // 定义邮箱格式的正则表达式
    const emailRegex = /^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;

    // 检查邮箱是否符合格式要求
    return emailRegex.test(email);
}

// 发送请求:获取验证码
function getCode(emailAddress){
    axios.get(`http://${host}:8080/AVR/logins?emailAddress=`+emailAddress)
}

// 发送请求:检查验证码是否正确
function checkCode(emailAddress, code){
    axios.post(`http://${host}:8080/AVR/logins/check`,{
        emailAddress:emailAddress,
        code:code
    }).then(response =>{ // 成功地回调
        console.log("返回结果====>" + response.data)
        if(response.data){
            sessionStorage.setItem("email",emailAddress)
            window.location = `http://${host}:8080/AVR/logins/1?emailAddress=`+emailAddress;
        }else{
            alert("验证不通过！！！")
        }

    })
}